package org.trc.network;

import java.awt.Rectangle;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.linearref.LengthIndexedLine;

/**
 * 
 * @author Lin Xu
 *
 */

// 弧段--起止节点
public class Edge extends Graphable {
	//基本属性
	public String id; // 路段ID唯一
	public String startNodeID; // 数字化起点
	public String endNodeID;// 数字化终点
	public int direction = 0; //  0：未调查,  1：双向  , 2：顺方向 3：逆方向 （数字化方向）
	public double length; //路段长度
	public LineString line; // 几何
	
	//线性参考（双向路）
	public String multiLinkID1;  
	public float startOffset1;
	public float endOffset1;
	
	public String multiLinkID2; 
	public float startOffset2;
	public float endOffset2;
	
	public String pathno; //所属道路ID (RoadID)
	public String pathname; //道路名 (RoadName)
	public String pathclass; //道路类别（快速路、主干道、次干道、支路、内部路）
	
	public double weight; //
	public int count;

	/*------------构造器---------------------*/
	public Edge(String edgeId, String startNodeId, String endNodeId, int direction, LineString line) {
		this.id = edgeId;
		this.startNodeID = startNodeId;
		this.endNodeID = endNodeId;
		this.line = line;
		if(line != null)
			this.length = line.getLength();
	}
	
	public Edge(String edgeId, String startNodeId, String endNodeId, LineString line,int direction, double weight) {
		this(edgeId, startNodeId, endNodeId, direction, line);
		this.weight = weight;
	}
	
	public Edge(String edgeId, String startNodeId, String endNodeId, int direction, LineString line, String pathno, String multilinkid, float soffset, float eoffset) {
		this(edgeId, startNodeId, endNodeId, direction, line);
//		this.direction = direction;
		this.pathno = pathno;
		this.multiLinkID1 = multilinkid;
		this.startOffset1 = soffset;
		this.endOffset1 = eoffset;
	}

	public Edge(String edgeId, String startNodeId, String endNodeId, int direction, float length, LineString line) {
		this(edgeId, startNodeId, endNodeId, direction, line);
		this.length = length;
	}

	/*------------GET/SET -----------------*/
	public void setLinearReference(String multiLinkID, float  startOffset,float endOffset){
		if(multiLinkID1 != null){
			this.multiLinkID2 = multiLinkID;
			this.startOffset2 = startOffset;
			this.endOffset2 = endOffset;
			return;
		}
		
		this.multiLinkID1 = multiLinkID;
		this.startOffset1 = startOffset;
		this.endOffset1 = endOffset;
			
	}
    
	public String getID() {
		return id;
	}

	//数字化起点
	public String getStartNodeId() {
		return startNodeID;
	}

	//数字化终点
	public String getEndNodeId() {
		return endNodeID;
	}

	//几何边
	public LineString getLineString() {
		return line;
	}

	//另一个节点
	public String getAnotherNodeId(String nodeId) {
		if (!nodeId.equals(endNodeID)) {
			return endNodeID;
		} else {
			return startNodeID;
		}
	}

	//是否是数字化起点
	public boolean isStartNode(String nodeId) {
		if (nodeId.equals(startNodeID)) {
			return true;
		} else {
			return false;
		}
	}
    
	public double getLinearDistance(String nodeID, Coordinate point) {
		if(isStartNode(nodeID))
			return new LengthIndexedLine(line).indexOf(point);
		else
			return length - new LengthIndexedLine(line).indexOf(point);
	}
	
	//外接矩形框
	public Rectangle getMBR() {
		Envelope en = line.getEnvelopeInternal();
		return new Rectangle((int) en.getMinX(), (int) en.getMinY(), (int) en.getWidth(), (int) en.getHeight());
	}

    public float getLinearStartOffest(String multiLinkID){
    	if(this.multiLinkID1.equals(multiLinkID))
    		return this.startOffset1;
    	else
    		return this.startOffset2;
    }
    
    public float getLinearEndOffset(String multiLinkID) {
    	if(this.multiLinkID1.equals(multiLinkID))
    		return this.endOffset1;
    	else
    		return this.endOffset2;    	   	
    }
    
}
